using System.Collections.Generic;
using System.Linq;
using NHibernate;
using Wikey.Domain.Components;
using Wikey.Entity;
using Wikey.Utilities.Extensions;

namespace Wikey.Domain.Repository
{
    public interface ISpaceRepository
    {
        List<Space> SelectAll();
        int Save(Space space);
        Space Find(string name);
        Space Find(int id);
    }

    public class SpaceRepository : Repository, ISpaceRepository
    {
        public SpaceRepository(ISessionSource source) : base(source)
        {
        }

        public SpaceRepository(ISession session) : base(session)
        {
        }

        public List<Space> SelectAll()
        {
            var spacesQuery = from spaces in Context().Spaces
                              select spaces;

            return spacesQuery.ToList();
        }

        public int Save ( Space space )
        {
            space.FriendlyUrlName = space.Name.UrlFriendlyName();
            base.Save(space);
            return space.Id; 
        }

        public Space Find(string name)
        {
            var spaceQuery = from space in Context().Spaces
                        where space.FriendlyUrlName.Equals(name)
                        select space;

            return spaceQuery.SingleOrDefault();
        }

        public Space Find(int id)
        {
            Space found = base.Find<Space>(id);

            return found;
        }
    }
}